<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>商品上架</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
		
	</head>
	<body>
		<table id="onselltable" lay-filter="onselltable"></table>
		<script type="text/html" id="onsellact">
			<a class="layui-btn layui-btn-sm" lay-event="onsell">上架</a>
		</script>
		<script src="../../layuiadmin/layui/layui.js"></script>  
		<script src="../../js/jquery-1.8.3.js"></script>
		<script src="../../js/user/common.js"></script>
		<script src="../../js/exchange/changetimetype.js"></script>
		<script src="../../js/jquery.cookie.js"></script>
		<script>
			layui.use(['table','layer','jquery'],function(){
				var table = layui.table,
				layer = layui.layer,
				$ = layui.$;
				
				table.render({
					elem:'#onselltable',
					url:urlHead2 + '/orderup/showLib',//数据接口
					page:true,//开启分页
                    headers:{
                        "Authorization":$.cookie('access_token')
                    },
					cols:[[//表头
						{field:'roomname',title:'房间名称',fixed:'left',width:100},
						{field:'sourceid',title:'房间来源',width:90},
						{field:'num',title:'房间数量',width:90},
						{field:'roomdrc',title:'房间描述',width:90},
						{field:'uptype',title:'上架类型',width:90},
						{field:'date',title:'使用日期',width:117,templet:function(d){//参数 d，包含接口返回的所有字段和数据
							var date = changetimetype(d.date);//转换时间格式
							return date;
						}},
						{field:'count',title:'上架房间数',edit:'text',width:104},
						{field:'begintime',title:'兑换开始时间',width:117,edit:'text'},
						{field:'endtime',title:'兑换截止时间',width:117,edit:'text'},
						{field:'exrate',title:'兑换比例',edit:'text',width:90},
						{fixed:'right',title:'操作',toolbar:'#onsellact',width:80}
					]],
					done:function(res){//res即为你接口返回的信息
						var result = res;
						console.log("返回的所有数据：")
						console.log(result);
						console.log("第一行数据：");
						console.log(result.data[0]);
						
						//监听工具条
						table.on('tool(onselltable)',function(obj){
							console.log(obj);
							var data = obj.data;//获得当前行数据
							var layEvent = obj.event; //获得 lay-event 对应的值
							var tr = obj.tr; //获得当前行 tr 的DOM对象
							
							console.log("上架行的数据：")
							console.log(data);
							console.log(layEvent);
							
							//当前行索引
							var index = JSON.stringify($("table").find("tbody").find(".layui-table-hover").data('index'));//获取上架的行数
							console.log("上架行的索引："+index);
							

							//当前行所有数据，包括没在表格里显示出来的
							var reslist = result.data[index];
							console.log("上架行数据：");
							console.log(reslist);
							
							if(layEvent === 'onsell'){//上架操作
								
								//所填项不能为空
								if(reslist.count==null){
									layer.msg('上架房间数不能为空');
									return false;
								}else if(reslist.begintime==null){
									layer.msg('兑换开始时间不能为空');
									return false;
								}else if(reslist.endtime==null){
									layer.msg('兑换截止时间不能为空');
									return false;
								}else if(reslist.exrate==null){
									layer.msg('兑换比例不能为空');
									return false;
								}
								
								//判断上架房间数
								if(reslist.count>0&&reslist.count<=reslist.num){
									console.log("父页面地址："+window.parent.location.href);
									
									var loc = window.parent.location.href;
									var n1 = loc.length;//地址的总长度
									var n2 = loc.indexOf("=");//取得=号的位置
									var upadminid = decodeURI(loc.substr(n2+1, n1-n2));//从=号后面的内容
									console.log("管理员id:"+upadminid);
									
									//转换使用日期的格式
									var usedate = changetimetype(reslist.date);
									
									console.log(reslist.count);
									//将字符型的上架数量转换成整形
									var onsellcount = parseInt(reslist.count);
									console.log(onsellcount);
									
									//定义对象
									var ordupRoomLibVo = {
										batchcode:reslist.batchcode,
										dateStr:usedate,
										num:reslist.num,
										picurl:reslist.picurl,
										roomdrc:reslist.roomdrc,
										roomid:reslist.roomid,
										roomname:reslist.roomname,
										roomstate:reslist.roomstate,
										sourceid:reslist.sourceid,
										uptype:reslist.uptype
									};
									
									var ordupExchangeLibVo = {
										upadminid:upadminid,
										sourceid:reslist.sourceid,
										begintime:reslist.begintime,
										endtime:reslist.endtime,
										exrate:reslist.exrate,
										count:onsellcount,
										usedateStr:usedate
									};
									
									var JSONObj = new Object();
									JSONObj.sod = JSON.stringify(ordupRoomLibVo);
									JSONObj.jsonstr = JSON.stringify(ordupExchangeLibVo);

									$.ajax({
										type:"post",
										url:urlHead2 + "/orderup/orderuproom",
										cache:true,
										headers:{
							        		"Authorization":$.cookie("access_token")
							   			},
							   			data:JSONObj,
										success:function(res){
											if(res.code==0){//上架成功
												layer.msg('上架成功',function(){
													tr.html('');
												});
											}else if(res.code==600602){
												layer.msg('用户未登录，请先登录',function(){
													location.replace('../../views/user/login_by_username_new.html');
												});
											}else if(res.code==600603){
												layer.msg('token已过期，请重新登录',function(){
													location.replace('../../views/user/login_by_username_new.html');
												});
											}else{
												layer.msg(res.msg);
											}
										},
										error:function(){
											location.href = "../error.html";
										},
										dataType:"json"
									});
								}else{//上架房间数大于房间数量
									layer.msg('上架房间数有误，请重新输入');
								}
								
							}
						});
							
						}
					});
					

			});
		</script>
	</body>
</html>
